package com.jyniubi.core.controller.admin;


import com.jyniubi.base.result.R;
import com.jyniubi.core.pojo.entity.Dict;
import com.jyniubi.core.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author jyzhang
 * @since 2024-04-27
 */
@Api(tags = "数据字典管理模块")
@RestController
@RequestMapping("/admin/core/dict")
@Slf4j
public class AdminDictController {

    @Resource
    private DictService dictService;

    @ApiOperation("根据parentId查询一组数据字典")
    @GetMapping("/dictsByParentId/{parentId}")
    public R dictsByParentId(@PathVariable("parentId") Long parentId){
        List<Dict> dicts = dictService.dictsByParentId(parentId);
        return R.ok().data("items",dicts);
    }
    @ApiOperation("删除数据字典")
    @DeleteMapping("/{pId}/{id}")
    public R deleteDictById(@PathVariable("id")String id,@PathVariable("pId")String pId){
        dictService.deleteDictById(pId,id);
        return R.ok();
    }
    @ApiOperation("根据id查询数据字典")
    @GetMapping("/getDictByid/{id}")
    public R getDictById(@PathVariable("id")String id){
        Dict dict = dictService.getById(id);
        return R.ok().data("item",dict);
    }
    @ApiOperation("编辑数据字段")
    @PutMapping("/update")
    public R update(@RequestBody Dict dict){
        dictService.updateByDictId(dict);
        return R.ok();
    }

    @ApiOperation("新增数据字典")
    @PostMapping("/insert")
    public R insert(@RequestBody Dict dict){
        dictService.saveDict(dict);
        return R.ok();
    }

    @ApiOperation("导入数据字段")
    @PostMapping("/import")
    public R importDicts(MultipartFile file){

        dictService.importDicts(file);
        return R.ok();
    }
    @ApiOperation("导出数据字段")
    @GetMapping("/export")
    public void exportDicts(HttpServletResponse response) {
        dictService.exportDicts(response);
    }
}

